package org.airline.action;

import java.util.List;

import javax.annotation.Resource;

import org.airline.common.IModuleName;
import org.airline.entity.PageBean;
import org.airline.entity.User;
import org.airline.service.UserService;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;

@Component("UserAction")
@SuppressWarnings("all")
public class UserAction extends SuperAction {

	private User user;
	private UserService userService;
	private String forwardURL;
	private IModuleName iModuleName;
	private String keyWord;
	private List<User> userList;
	private int page = 1;
	private String random = "";
	private int pageSize = 5;
	private PageBean pageBean;

	// 用户注册
	public String userRegister() throws Exception {
		try {
			this.userService.saveUser(user);
		} catch (Exception e) {
			super.logFailureString(iModuleName.ADD, iModuleName.ADD, "用户注册失败",
					e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.ADD, iModuleName.ADD, "用户注册成功");
		forwardURL = "UserAction!login";
		return "userInfo";
	}

	// 普通用户登录
	public String login() throws Exception {
		try {
			user = userService
					.checkUser(user.getUserName(), user.getPassword());
			if (user != null) {
				ServletActionContext.getRequest().getSession().setAttribute(
						"Logined", user);
				forwardURL = "/";
			} else {
				forwardURL = "/jsp/common/loginError.jsp";
			}
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET, "登录失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET, "登录成功");
		return SUCCESS;
	}

	// 管理员登陆
	public String adminLogin() throws Exception {
		try {
			user = userService
					.checkUser(user.getUserName(), user.getPassword());
			if (user != null) {
				ServletActionContext.getRequest().getSession().setAttribute(
						"Logined", user);
				forwardURL = "/admin/default.jsp";
			} else {
				forwardURL = "/jsp/common/loginError.jsp";
			}
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET, "登录失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET, "登录成功");
		return SUCCESS;
	}
	
	// 后台修改用户信息
	public String updateUserInfo() throws Exception {
		try {
			this.userService.updateUser(this.user);
		} catch (Exception e) {
			super.logFailureString(iModuleName.UPDATE, iModuleName.UPDATE,
					"修改用户信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.UPDATE, iModuleName.UPDATE,
				"修改用户信息成功");
		this.forwardURL = "UserAction!getAllUser";
		return "userInfo";
	}
	
	// 普通用户修改个人信息
	public String updateSelfInfo() throws Exception {
		try {
			this.userService.updateUser(this.user);
		} catch (Exception e) {
			super.logFailureString(iModuleName.UPDATE, iModuleName.UPDATE,
					"修改用户信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.UPDATE, iModuleName.UPDATE,
				"修改用户信息成功");
		this.forwardURL = "UserAction!getUserInfoById";
		return "userInfo";
	}

	// 根据id删除用户信息
	public String deleteUserById() throws Exception {
		try {
			this.userService.deleteUserById(this.user.getId());
		} catch (Exception e) {
			super.logFailureString(iModuleName.DELETE, iModuleName.DELETE,
					"根据id删除用户信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.DELETE, iModuleName.DELETE,
				"根据id删除用户信息成功");
		this.forwardURL = "UserAction!getAllUser";
		return "userInfo";
	}

	// 查询所有用户信息并分页显示
	public String getAllUser() throws Exception {
		try {
			pageBean = userService.queryForPage(pageSize, page);
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET,
					"查询所有用户信息并分页显示失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET,
				"查询所有用户信息并分页显示成功");
		forwardURL = "/admin/showAllUser.jsp";
		return SUCCESS;
	}

	// 根据id获取用户信息
	public String getUserInfoById() throws Exception {
		try {
			user = this.userService.getUserInfoById(user.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据id获取用户信息失败",
					e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据id获取用户信息成功");
		forwardURL = "/jsp/showUserInfo.jsp";
		return SUCCESS;
	}
	// 后台根据id获取用户信息
	public String getUserInfoByUserId() throws Exception {
		try {
			user = this.userService.getUserInfoById(user.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据id获取用户信息失败",
					e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据id获取用户信息成功");
		forwardURL = "/admin/showUserInfo.jsp";
		return SUCCESS;
	}

	// 根据关键字分页查询用户信息
	public String getUsersByKeyWords() throws Exception {
		try {
			pageBean = userService.queryForPageByKeyWords(keyWord, pageSize,
					page);
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET,
					"根据关键字分页查询用户信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET,
				"根据关键字分页查询用户信息成功");
		forwardURL = "/admin/showAllUser.jsp";
		return SUCCESS;
	}
	
	// 安全退出
	public String logout() throws Exception {
		ServletActionContext.getRequest().getSession().removeAttribute("Logined");
		forwardURL = "/jsp/airline.jsp";
		return SUCCESS;
	}
	
	// 管理员退出
	public String AdminLogout() throws Exception {
		ServletActionContext.getRequest().getSession().removeAttribute("Logined");
		forwardURL = "/admin/adminLogin.jsp";
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	@Resource(name = "UserService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getForwardURL() {
		return forwardURL;
	}

	public void setForwardURL(String forwardURL) {
		this.forwardURL = forwardURL;
	}

	public IModuleName getiModuleName() {
		return iModuleName;
	}

	public void setiModuleName(IModuleName iModuleName) {
		this.iModuleName = iModuleName;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

}
